package com.quan.bookmall.service.impl;

import com.quan.bookmall.dto.BookDTO;
import com.quan.bookmall.entity.Book;
import com.quan.bookmall.repository.BookRepository;
import com.quan.bookmall.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;

@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
    
    private final BookRepository bookRepository;
    
    @Override
    public BookDTO findById(Long id) {
        Book book = bookRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("书籍不存在"));
        return convertToDTO(book);
    }
    
    @Override
    public Page<BookDTO> findAll(Pageable pageable) {
        return bookRepository.findAll(pageable).map(this::convertToDTO);
    }
    
    @Override
    public Page<BookDTO> findByName(String name, Pageable pageable) {
        return bookRepository.findByNameContaining(name, pageable).map(this::convertToDTO);
    }
    
    @Override
    @Transactional
    public BookDTO create(BookDTO bookDTO) {
        Book book = new Book();
        BeanUtils.copyProperties(bookDTO, book);
        Book savedBook = bookRepository.save(book);
        return convertToDTO(savedBook);
    }
    
    @Override
    @Transactional
    public BookDTO update(Long id, BookDTO bookDTO) {
        Book book = bookRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("书籍不存在"));
        
        // 保留原有库存，避免直接覆盖
        Integer originalStock = book.getStock();
        
        BeanUtils.copyProperties(bookDTO, book);
        book.setId(id); // 确保ID不被修改
        
        // 如果没有提供新库存，则保留原有库存
        if (bookDTO.getStock() == null) {
            book.setStock(originalStock);
        }
        
        Book updatedBook = bookRepository.save(book);
        return convertToDTO(updatedBook);
    }
    
    @Override
    @Transactional
    public void delete(Long id) {
        if (!bookRepository.existsById(id)) {
            throw new EntityNotFoundException("书籍不存在");
        }
        bookRepository.deleteById(id);
    }
    
    @Override
    public boolean existsByIsbn(String isbn) {
        return bookRepository.existsByIsbn(isbn);
    }
    
    @Override
    @Transactional
    public boolean updateStock(Long id, Integer quantity) {
        Book book = bookRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("书籍不存在"));
        
        if (book.getStock() < Math.abs(quantity) && quantity < 0) {
            return false; // 库存不足
        }
        
        book.setStock(book.getStock() + quantity);
        bookRepository.save(book);
        return true;
    }
    
    private BookDTO convertToDTO(Book book) {
        BookDTO bookDTO = new BookDTO();
        BeanUtils.copyProperties(book, bookDTO);
        return bookDTO;
    }
} 